package com.service.impl;

import com.common.exception.db.InsertException;
import com.dao.AppraiseDao;
import com.entity.AppraiseInfo;
import com.service.AppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AppraiseServiceimpl implements AppraiseService {

    @Autowired
    private AppraiseDao appraiseDao;

    @Override
    public List<AppraiseInfo> LookAppraise(Integer type, Integer getUserId,Integer limit) {
        return appraiseDao.LookAppraise(type,getUserId,limit);
    }

    @Override
    @Transactional
    public int insertAppraise(Integer recordId,Integer type,Integer sendUserId,Integer getUserId,Integer appraiseNum,Integer serviceAttitude,Integer teachingQuality,Double average,Integer num,String appraiseContent) {
        int count = appraiseDao.insertAppraise(sendUserId,getUserId,appraiseNum,serviceAttitude,teachingQuality,average,appraiseContent);
        if(count > 0){
            count = appraiseDao.updateRecordAppraise(recordId,type);
            if(count > 0){
                if(num == 1){
                   count = appraiseDao.updateTeacherValue(getUserId,1);
                   if(count > 0){
                       return count;
                   }else{
                       throw new InsertException("修改失败");
                   }
                }else if (num == 3){
                    int value = appraiseDao.findTeacherValue(getUserId);
                    if(value == 0){
                        return count;
                    }else{
                        count = appraiseDao.updateTeacherValue(getUserId,2);
                        if(count > 0){
                            return count;
                        }else{
                            throw new InsertException("修改失败");
                        }
                    }
                }
                return count;
            }else{
                throw new InsertException("修改评价状态失败");
            }
        }else {
            throw new InsertException("新增评价失败");
        }
    }

    @Override
    public int showHiddenResume(Integer type,Integer userId) {
        return appraiseDao.showHiddenResume(type,userId);
    }

    @Override
    public int LookIsAppraise(Integer recordId) {
        return appraiseDao.LookIsAppraise(recordId);
    }
}
